package com.ims.web.controller.system;

import com.ims.common.core.controller.BaseController;
import com.ims.common.core.domain.AjaxResult;
import com.ims.common.core.page.TableDataInfo;
import com.ims.common.core.redis.RedisCache;
import com.ims.system.domain.SysCity;
import com.ims.system.service.ISysCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 城市Controller
 *
 * @author IMS
 * @date 2023-05-18
 */
@Api(tags = "SysCityController", description = "城市")
@RestController
@RequestMapping("/system/city")
public class SysCityController extends BaseController
{
    @Autowired
    private ISysCityService sysCityService;

    @Autowired
    private RedisCache redisCache;

    public static final String CITY_REDIS_CACHE = "city_redis_cache";

    /**
     * 查询城市列表
     */
    @ApiOperation("查询城市列表")
    // @PreAuthorize("@ss.hasPermi('system:city:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysCity sysCity)
    {
        startPage();
        List<SysCity> list = sysCityService.selectSysCityList(sysCity);
        return getDataTable(list);
    }

    /**
     * 查询省列表
     */
    @ApiOperation("查询省")
    @GetMapping("/provinceList")
    public AjaxResult provinceList(SysCity sysCity) {
        sysCity.setParentId(1L);
        sysCity.setLevel("2");
        return AjaxResult.success(sysCityService.selectSysCityList(sysCity));
    }

    /**
     * 查询城市列表
     */
    @ApiOperation("查询城市")
    @GetMapping("/cityList")
    public AjaxResult cityList(SysCity sysCity) {
        sysCity.setParentId(sysCity.getParentId());
        sysCity.setLevel("3");
        return AjaxResult.success(sysCityService.selectSysCityList(sysCity));
    }

    /**
     * 查询区县列表
     */
    @ApiOperation("查询区县")
    @GetMapping("/districtList")
    public AjaxResult districtList(SysCity sysCity) {
        sysCity.setParentId(sysCity.getParentId());
        sysCity.setLevel("4");
        return AjaxResult.success(sysCityService.selectSysCityList(sysCity));
    }

    /**
     * 查询城市列表(树形)
     */
    @ApiOperation("查询城市列表（树形）")
    // @PreAuthorize("@ss.hasPermi('system:city:list')")
    @GetMapping("/tree")
    public AjaxResult tree(SysCity sysCity)
    {
        List<Map> returnList = new ArrayList<>();
        List<SysCity> list = sysCityService.selectSysCityList(null);
        if (!Objects.isNull(sysCity.getParentId())) {
            if (!CollectionUtils.isEmpty(list)) {
                for (SysCity r : list) {
                    Map region = new HashMap();
                    region.put("value", r.getId());
                    region.put("label", r.getName());
                    returnList.add(region);
                }
            }
            return AjaxResult.success(returnList);
        }

        if (!CollectionUtils.isEmpty(list)) {
            for (SysCity province : list) {
                if (province.getParentId() == 1L) {
                    Map pro = new HashMap();
                    pro.put("value",province.getId());
                    pro.put("label",province.getName());
                    List<Map> cityList = new ArrayList<>();
                    for (SysCity city : list) {
                        if (city.getParentId().equals(province.getId())) {
                            Map ct = new HashMap();
                            ct.put("value",city.getId());
                            ct.put("label",city.getName());
                            List<Map> regionList = new ArrayList<>();
                            for (SysCity region : list) {
                                if (region.getParentId().equals(city.getId())) {
                                    Map re = new HashMap();
                                    re.put("value",region.getId());
                                    re.put("label",region.getName());
                                    regionList.add(re);
                                }
                            }
                            ct.put("children",regionList);
                            cityList.add(ct);
                        }
                    }
                    pro.put("children",cityList);
                    returnList.add(pro);
                }
            }
        }
        redisCache.setCacheObject(CITY_REDIS_CACHE, returnList,24*60, TimeUnit.MINUTES);
        return AjaxResult.success(returnList);
    }

}
